package Logica;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ClienteTCP extends Thread {

	ObjectInputStream in = null;
	ObjectOutputStream out = null;
	Principal serv;
	Socket sockcliente;
	boolean logado;
	String ultpedido;

	public ClienteTCP(Principal serv, Socket sockcliente) {
		super();
		this.serv = serv;
		this.sockcliente = sockcliente;
		logado = false;
		ultpedido = null;

		try {
			in = new ObjectInputStream(sockcliente.getInputStream());
			out = new ObjectOutputStream(sockcliente.getOutputStream());
		} catch (IOException ex) {
			Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null,
					ex);
		}
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		String resposta = null, tipo = null, msg;
		StringTokenizer st = null;

		while (!logado) {
			try {
				msg = (String) in.readObject();
				System.out.println(msg);
				st = new StringTokenizer(msg);
				tipo = st.nextToken();

				if (tipo.equalsIgnoreCase("LOGIN")) {
					out.writeObject(true);
					out.flush();
				} else if (tipo.equalsIgnoreCase("DESCONECTAR")) {
					System.out.println("Close");
					sockcliente.close();
					break;
				}

			} catch (Exception e) {
				System.out.println(e);
				try {
					System.out.println("Close");
					sockcliente.close();
					break;
				} catch (IOException ex) {
					Logger.getLogger(Principal.class.getName()).log(
							Level.SEVERE, null, ex);
				}
			}
		}
	}

}
